<?php
namespace app\index\controller;
use app\index\model\Archives;
use app\index\model\ArchivesCategory;
use app\index\model\Archives_file;
use app\index\model\Arc_extend_info;
use app\index\model\Storeroom;
use app\index\model\Tag;
use app\index\model\TagArchivesRelation;
use app\index\model\TagGroup;
use think\Controller;
use think\Db;
use think\Request;
use think\Session;

class Query extends Common {
	protected $request;
	protected $ac_model;
	protected $a_model;
	protected $acf_model;
	protected $tag_model;
	protected $tag_group_model;
	protected $tag_archives_relation_model;
	protected $storage_model;
	protected $aei_model;

	public function __construct(Request $request = null) {
		parent::__construct($request);
		$this->request = $request;
		$this->ac_model = new ArchivesCategory();
		$this->a_model = new Archives();
		$this->tag_model = new Tag();
		$this->tag_archives_relation_model = new TagArchivesRelation();
		$this->tag_group_model = new TagGroup();
		$this->storage_model = new Storeroom();
		$this->acf_model = new Archives_file();
		$this->aei_model = new Arc_extend_info();

	}
	// 档案搜索2
	// public function index() {
	// 	$res = scandir("./folder/2/");
	// 	foreach ($res as $key => $value) {
	// 		$data[$key]['name'] = $res[$key];
	// 		$suffix[$key] = substr(strrchr($data[$key]['name'], '.'), 1);
	// 		if ($suffix[$key] == '0') {
	// 			$data[$key]['type'] = 'dir';
	// 			$data[$key]['suffix'] = 'dir';
	// 		} else {
	// 			$data[$key]['type'] = 'file';
	// 			$data[$key]['suffix'] = $suffix[$key];

	// 			$rev[$key] = Db::table('cn_archives')
	// 				->alias('ca')
	// 				->join('cn_archives_file caf', 'caf.arc_id = ca.arc_id')
	// 				->join('cn_file_log cfl', 'caf.file_id = cfl.file_id')
	// 				->join('cn_archives_category cac', 'cac.cate_id = ca.category_id')
	// 				->where('ca.arc_name', $data[$key]['name'])
	// 				->field('ca.arc_id,arc_name,arc_type,cfl.file_save_path,ca.mark')
	// 				->select();
	// 		}

	// 	}
	// 	unset($data[0]);
	// 	unset($data[1]);
	// 	return view('index@Query/index', [
	// 		'res' => $data,
	// 	]);
	// }
	// public function getDirname($id) {
	// 	$res = scandir("./folder/2/" . $id . "/");
	// 	foreach ($res as $key => $value) {
	// 		$data[$key]['name'] = $res[$key];
	// 		$suffix[$key] = substr(strrchr($data[$key]['name'], '.'), 1);

	// 		if ($suffix[$key] == false) {
	// 			$data[$key]['type'] = 'dir';
	// 			$data[$key]['suffix'] = 'dir';
	// 		} else {
	// 			$data[$key]['type'] = 'file';
	// 			$data[$key]['suffix'] = $suffix[$key];

	// 			//$data[$key]['realname'] = basename($data[$key]['name'], "." . $suffix[$key]);

	// 			$data[$key]['realname'] = str_replace('.' . $suffix[$key], '', $data[$key]['name']);
	// 			$rev[$key] = Db::table('cn_archives')
	// 				->alias('ca')
	// 				->join('cn_archives_file caf', 'caf.arc_id = ca.arc_id')
	// 				->join('cn_file_log cfl', 'caf.file_id = cfl.file_id')
	// 				->join('cn_archives_category cac', 'cac.cate_id = ca.category_id')
	// 				->where('ca.arc_name', $data[$key]['realname'])
	// 				->field('ca.arc_id,arc_title,arc_name,arc_type,cfl.file_save_path,ca.mark')
	// 				->find();

	// 			$data[$key]['arc_id'] = $rev[$key]['arc_id'];
	// 			$data[$key]['arc_name'] = $rev[$key]['arc_name'];
	// 			$data[$key]['arc_type'] = $rev[$key]['arc_type'];
	// 			$data[$key]['arc_title'] = $rev[$key]['arc_title'];
	// 			$data[$key]['file_save_path'] = $rev[$key]['file_save_path'];
	// 			$data[$key]['mark'] = $rev[$key]['mark'];

	// 		}

	// 	}

	// 	unset($data[0]);
	// 	unset($data[1]);

	// 	foreach ($data as $k => $v) {
	// 		$rrr = $this->a_model->getDataByCondition(array('arc_id' => $data[$k]['arc_id']));
	// 		$filePath[$k] = $this->getFilePathUrl($rrr['category_id'], 2);
	// 		$filePath[$k] = $filePath[$k]['path'] . $rrr['arc_name'] . '.' . $rrr['arc_type'];
	// 		$data[$k]['path'] = $filePath[$k];
	// 	}
		
	// 	return view('index@Query/dir', [
	// 		'res' => $data,
	// 	]);
	// }

	public function index(){
		$data = $this->ac_model->where('cate_type',2)->where('cate_parent_id',0)->where('is_delete',0)->select();
		return view('index@Query/index', [
			'res' => $data,
			'file' => array()
		]);
	}

	public function getDirname($id){
		$data = $this->ac_model->where('cate_type',2)->where('cate_parent_id',$id)->where('is_delete',0)->select();
		$sql = "SELECT 
					inf.arc_id,ar.arc_title,ar.arc_type,fl.file_old_name,fl.file_save_path
				FROM
					cn_interfile AS inf,cn_archives AS ar,cn_archives_file AS arf,cn_file_log AS fl
				WHERE
					inf.arc_id = ar.arc_id AND inf.arc_id = arf.arc_id AND arf.file_id = fl.file_id AND inf.interfile_status = '3' AND inf.interfile_cate_id = '".$id."'
				";
		$data2 = Db::query($sql);
		return view('index@Query/index',[
			'res' => $data,
			'file' => $data2
		]);
	}

	

	// function cycle($data, $key, $pkey, $name) {
	// 	$cate_id = session('cate_id');
	// 	$tree = '<ul>';
	// 	$jstree = '';
	// 	foreach ($data as $k => $v) {
	// 		if ($v[$key] == $cate_id) {
	// 			$jstree = '{"selected":true}';
	// 		} else {
	// 			$jstree = '';
	// 		}
	// 		$tree .= '<li class="tree_list jstree-open" data-id="' . $v[$key] . '" data-jstree=\'' . $jstree . '\'>' . $v[$name];
	// 		if ($v[$pkey] != 0) {
	// 			$tree .= createCateTree($v[$pkey], $key, $pkey, $name);
	// 		}
	// 		$tree .= '</li>';
	// 	}
	// 	$tree .= '</ul>';

	// 	return $tree;
	// }
	// public function doSearch()
	// {
	// 	$rev = new Search_model;
	//     $res = $rev->getSearch();
	// 	return view('index@Search/result',[
	// 		'res' => $res
	// 	]);
	// }

	//在线阅读
	public function onRead() {
		$params = array();
		$id = $this->request->param('id', 0);
		if (empty($id)) {
			jsonOut(0, '参数错误');
		}
		$data = $this->a_model->getDataByCondition(array('arc_id' => $id));
		$extend_data = $this->aei_model->where(array('arc_id' => $id))->find();

		if (empty($data)) {
			jsonOut(0, '文件不存在');
		}

		$filePath = $this->getFilePathUrl($data['category_id'], 2);
		// dump($filePath['path']);
		// dump($data['arc_name']);
		//文件夹还有问题  先写死
		$filePath = $filePath['path'] . $data['arc_name'] . '.' . $data['arc_type'];
		$data['filePath'] = $filePath;
		$data['http'] = 'http://' . $_SERVER['HTTP_HOST'];
		$data['box_path'] = $this->storage_model->getStoragePath($data['box_id']);
		$data['arc_time'] = date('Y-m-d', strtotime($data['arc_time']));
		$params['data'] = $data;
		$params['extend_data'] = $extend_data;
		$ac_data = $this->storage_model->where(array('is_delete' => 0))->select();
		$cate_tree = recursionStorage($ac_data, 'sto_id', 0, 'patent_id');
		$tree = createStorageTree($cate_tree, 'sto_id', 'patent_id', 'sto_name');
		if (empty($tree)) {
			jsonOut(0, '文件目录错误');
		}
		$params['tree'] = $tree;
		return view('index@Query/onRead', $params);
	}

	//编辑文档信息
	public function setArc($mark) {
		if ($this->request->instance()->isPost()) {
			$id = $this->request->post('id');
			$arc_title = $this->request->post('arc_title');
			$arc_reference = $this->request->post('arc_reference');
			// $arc_class_num = $this->request->post('arc_class_num');
			// $arc_file_num = $this->request->post('arc_file_num');
			// $arc_total_num = $this->request->post('arc_total_num');
			// $arc_part_num = $this->request->post('arc_part_num');
			// $arc_num = $this->request->post('arc_num');
			$arc_establishment_organization = $this->request->post('arc_establishment_organization');
			$arc_establishment_person = $this->request->post('arc_establishment_person');
			$arc_year = $this->request->post('arc_year');
			$arc_rank = $this->request->post('arc_rank');
			$arc_entities_num = $this->request->post('arc_entities_num');
			$arc_borrowed = $this->request->post('arc_borrowed');
			$arc_page_num = $this->request->post('arc_page_num');
			$box_id = $this->request->post('box_id');
			$arc_storage_position = $this->request->post('arc_storage_position');
			$arc_remarks = $this->request->post('arc_remarks');
			$arc_time = $this->request->post('arc_time');
			$arc_time_limit = $this->request->post('arc_time_limit');
			$data = $this->a_model->getDataByCondition(array('arc_id' => $id));
			$code = Session::get('code');
			Session::delete('code');
			if (empty($data)) {
				jsonOut(0, '档案信息错误');
			}
			//先查询档案盒里面是否已经满了
			$h_data = $this->storage_model->getDataByCondition(array('sto_id' => $box_id));
			if (empty($h_data)) {
				jsonOut(0, '档案盒不能为空');
			}
			$h_sy = $h_data['sto_available_pages'] - $h_data['to_available_pages'];
			if ($arc_page_num > $h_sy) {
				jsonOut(0, '档案盒最大还可以存放' . $h_sy . '页数据');
			}
			if ($mark == '1') {
				$save_data = array(
					'arc_title' => $_POST['arc_title'],
					'arc_reference' => $arc_reference,
					// 'arc_class_num'=>$arc_class_num,
					// 'arc_file_num'=>$arc_file_num,
					// 'arc_total_num'=>$arc_total_num,
					// 'arc_part_num'=>$arc_part_num,
					// 'arc_num'=>$arc_num,
					'arc_establishment_organization' => $arc_establishment_organization,
					'arc_establishment_person' => $arc_establishment_person,
					'arc_year' => $arc_year,
					'arc_rank' => $arc_rank,
					'arc_entities_num' => $arc_entities_num,
					'arc_borrowed' => $arc_borrowed,
					'box_id' => $box_id,
					'arc_storage_position' => $arc_storage_position,
					'arc_remarks' => $arc_remarks,
					'arc_time' => $arc_time . " 00:00:00",
					'arc_time_limit' => $arc_time_limit,
					'arc_page_num' => $arc_page_num,
				);
				$save = $this->a_model->updateByCondition(array('arc_id' => $id), $save_data);

			} elseif ($mark == '2') {
				$save_data = array(
					'arc_title' => $_POST['arc_title'],
					'arc_reference' => $arc_reference,
					// 'arc_class_num'=>$arc_class_num,
					// 'arc_file_num'=>$arc_file_num,
					// 'arc_total_num'=>$arc_total_num,
					// 'arc_part_num'=>$arc_part_num,
					// 'arc_num'=>$arc_num,
					'arc_establishment_organization' => $arc_establishment_organization,
					'arc_establishment_person' => $arc_establishment_person,
					'arc_year' => $arc_year,
					'arc_rank' => $arc_rank,
					'arc_entities_num' => $arc_entities_num,
					'arc_borrowed' => $arc_borrowed,
					'box_id' => $box_id,
					'arc_storage_position' => $arc_storage_position,
					'arc_remarks' => $arc_remarks,
					'arc_time' => $arc_time . " 00:00:00",
					'arc_time_limit' => $arc_time_limit,
					'arc_page_num' => $arc_page_num,
				);
				$extend_data = array(
					'arc_id' => $id,
					'ptime' => $_POST['ptime'],
					'pcha' => $_POST['pcha'],
					'pkey' => $_POST['pkey'],
					'pbrief' => $_POST['pbrief'],
					'pfrom' => $_POST['pfrom'],
					// 'psource' => $_POST['psource'],
					'add_time' => date('Y-m-d H:i:s'),
				);
				$save = $this->a_model->updateByCondition(array('arc_id' => $id), $save_data);
				$extend = $this->aei_model->updateByCondition(array('arc_id' => $id), $extend_data);

			} elseif ($mark == '3') {
				$save_data = array(
					'arc_title' => $_POST['arc_title'],
					'arc_reference' => $arc_reference,
					// 'arc_class_num'=>$arc_class_num,
					// 'arc_file_num'=>$arc_file_num,
					// 'arc_total_num'=>$arc_total_num,
					// 'arc_part_num'=>$arc_part_num,
					// 'arc_num'=>$arc_num,
					'arc_establishment_organization' => $arc_establishment_organization,
					'arc_establishment_person' => $arc_establishment_person,
					'arc_year' => $arc_year,
					'arc_rank' => $arc_rank,
					'arc_entities_num' => $arc_entities_num,
					'arc_borrowed' => $arc_borrowed,
					'box_id' => $box_id,
					'arc_storage_position' => $arc_storage_position,
					'arc_remarks' => $arc_remarks,
					'arc_time' => $arc_time . " 00:00:00",
					'arc_time_limit' => $arc_time_limit,
					'arc_page_num' => $arc_page_num,
				);

				$extend_data = array(
					'arc_id' => $id,
					'vtime' => $_POST['vtime'],
					'vformat' => $_POST['vformat'],
					'vmin' => $_POST['vmin'],
					'vkey' => $_POST['vkey'],
					'vbrief' => $_POST['vbrief'],
					'vfrom' => $_POST['vfrom'],
					// 'vsource' => $_POST['vsource'],
					'add_time' => date('Y-m-d H:i:s'),
				);
				$save = $this->a_model->updateByCondition(array('arc_id' => $id), $save_data);
				$extend = $this->aei_model->updateByCondition(array('arc_id' => $id), $extend_data);

			}

			if (isset($save) || isset($extend)) {

				//先把之前的档案盒页数开放
				$old_sto_data = $this->storage_model->getDataByCondition(array('sto_id' => $data['box_id']));
				$this->storage_model->updateByCondition(array('sto_id' => $data['box_id']), array('to_available_pages' => $old_sto_data['to_available_pages'] - $arc_page_num));
				//在把对应的档案盒页数减少
				$this->storage_model->updateByCondition(array('sto_id' => $box_id), array('to_available_pages' => $h_data['to_available_pages'] + $arc_page_num));
				jsonOut(1, '文档信息编辑成功');
			} else {
				jsonOut(0, '文档信息编辑失败');
			}
		}
	}

}
